package com.pig4cloud.pigx.contract.config.contract;

import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 合约模块配置
 *
 * @since 2022/8/27 14:23
 */
@Data
@Component
@ConfigurationProperties(prefix = "ripple.contract")
public class ContractConfig implements InitializingBean {
    /**
     * 订单设置
     */
    private ContractOrderConfig order;

    /**
     * 仓位设置
     */
    private ContractPositionConfig position;

    /**
     * 深度图设置
     */
    private ContractDepthMapConfig depthMap;

    /**
     * 定时器设置
     */
    private ContractScheduledConfig scheduled;

    /**
     * 止盈止损设置
     */
    private ContractStopOrderConfig stopOrder;

    /**
     * websocket设置
     */
    private ContractWebsocketConfig websocket;

    /**
     * 设置属性后，校验属性是否都设置上了
     *
     * @since 2022/8/27 18:53
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        // 校验订单设置
        validOrder();
        // 校验仓位设置
        validPosition();
        // 校验止盈止损设置
        validStopOrder();
    }

    /**
     * 校验订单设置
     *
     * @since 2022/8/30 15:18
     */
    private void validOrder() {
        if (Objects.isNull(order.getMarketCancelDelay())) {
            throw new RuntimeException("marketCancelDelay不能为空");
        }
        if (order.getMarketCancelDelay() < 0) {
            throw new RuntimeException("marketCancelDelay不能小于0");
        }

        if (Objects.isNull(order.getExpire())) {
            throw new RuntimeException("limitExpireDelay不能为空");
        }
        if (order.getExpire() < 0) {
            throw new RuntimeException("limitExpireDelay不能小于0");
        }
    }

    /**
     * 校验仓位设置
     *
     * @since 2022/8/30 15:18
     */
    private void validPosition() {
        if (Objects.isNull(position.getLimitWhenDispatchLiquidate())) {
            throw new RuntimeException("liquidateMaxSizeWhenDispatch不能为空");
        }
        if (position.getLimitWhenDispatchLiquidate() <= 0) {
            throw new RuntimeException("liquidateMaxSizeWhenDispatch不能小于等于0");
        }

        if (Objects.isNull(position.getMarginRateEmailTrigger())) {
            throw new RuntimeException("marginRateEmailTrigger不能为空");
        }
        if (position.getMarginRateEmailTrigger().compareTo(BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("marginRateEmailTrigger不能小于等于0");
        }

        if (Objects.isNull(position.getLimitWhenDispatchMarginCheck())) {
            throw new RuntimeException("limitWhenDispatchMarginCheck不能为空");
        }
        if (position.getLimitWhenDispatchMarginCheck() <= 0) {
            throw new RuntimeException("LimitWhenDispatchMarginCheck不能小于等于0");
        }

        if (Objects.isNull(position.getLimitWhenDispatchCrossCalculate())) {
            throw new RuntimeException("limitWhenDispatchCrossCalculate不能为空");
        }
        if (position.getLimitWhenDispatchCrossCalculate() <= 0) {
            throw new RuntimeException("limitWhenDispatchCrossCalculate不能小于等于0");
        }
    }

    /**
     * 校验止盈止损设置
     *
     * @since 2022/8/30 15:18
     */
    private void validStopOrder() {
        if (Objects.isNull(stopOrder.getLimitWhenDispatchStopOrderCheck())) {
            throw new RuntimeException("limitWhenDispatchStopOrderCheck不能为空");
        }
        if (stopOrder.getLimitWhenDispatchStopOrderCheck() <= 0) {
            throw new RuntimeException("limitWhenDispatchStopOrderCheck不能小于等于0");
        }

        if (Objects.isNull(stopOrder.getLimitWhenDispatchStopOrder())) {
            throw new RuntimeException("limitWhenDispatchStopOrder不能为空");
        }
        if (stopOrder.getLimitWhenDispatchStopOrder() <= 0) {
            throw new RuntimeException("limitWhenDispatchStopOrder不能小于等于0");
        }
    }
}
